//
//  YKDropdownLabel.m
//  YunKang
//
//  Created by leoking870 on 2017/9/29.
//  Copyright © 2017年 leoking870. All rights reserved.
//

#import "YKDropdownLabel.h"
#import "YKSelectDropdownView.h"
@interface YKDropdownLabel ()
@property (nonatomic, strong) UILabel *detailLabel;
@property (nonatomic, strong) UIImageView *arrowImageView;
@property (nonatomic, copy)void(^selectionHandler)(YKDropdownLabel* view, NSInteger index, id<YKDropdown> text);
@end

@implementation YKDropdownLabel

-(instancetype)initWithText:(id<YKDropdown>)text row:(NSArray<YKDropdown> *)data selectionHandler:(void (^)(YKDropdownLabel* view, NSInteger, id<YKDropdown>))selectionHandler
{
    self = [super init];
    if (self) {
        [self addSubview:self.detailLabel];
        [self addSubview:self.arrowImageView];
        self.text = text;
        self.data = data;
        
        self.selectionHandler = selectionHandler;
        [self.arrowImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.trailing.equalTo(self.mas_trailing).offset(-8);
            make.centerY.equalTo(self.mas_centerY);
        }];
        [self.detailLabel setContentHuggingPriority:UILayoutPriorityFittingSizeLevel forAxis:UILayoutConstraintAxisHorizontal];
        [self.detailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.equalTo(self.mas_leading).offset(8);
            make.centerY.equalTo(self.mas_centerY);
            make.top.equalTo(self.mas_top); make.trailing.equalTo(self.arrowImageView.mas_leading).offset(-8);
        }];
        
        self.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onSelectViewTapped:)];
        [self addGestureRecognizer:tap];
    }
    return self;
}


- (BOOL)enabled {
    return self.userInteractionEnabled;
}

- (void)setEnabled:(BOOL)enabled {
    self.userInteractionEnabled = enabled;
}

- (void)onSelectViewTapped:(UITapGestureRecognizer *)sender {
    
    [YKSelectDropdownView showWithOriginView:sender.view strings:self.data initialSelection:[self.data indexOfObject:self.text] selectHandler:^(NSUInteger index) {
        self.text = self.data[index];
    }];
}

- (void)setText:(id<YKDropdown>)text {
    _text = text;
    _detailLabel.text = text.name;
    if (self.selectionHandler) {
        self.selectionHandler(self, [self.data indexOfObject:text], text);
    }
    
}



- (UILabel *)detailLabel {
    if (!_detailLabel) {
        _detailLabel = [[UILabel alloc] init];
        _detailLabel.textColor = [UIColor tf_colorWithHexString:@"#444444"];
        _detailLabel.font = [UIFont systemFontOfSize:14];
    }
    return _detailLabel;
}
- (UIImageView *)arrowImageView {
    if (!_arrowImageView) {
        _arrowImageView = [[UIImageView alloc] init];
        _arrowImageView.contentMode = UIViewContentModeScaleAspectFill;
        _arrowImageView.clipsToBounds = YES;
        _arrowImageView.image = [UIImage imageNamed:@"arrow-down-b"];
    }
    return _arrowImageView;
}


+ (NSArray<YKDropdownLabel *> *)dropdownLabelsFromData:(NSArray<YKDropdown> *)data selectionHandler:(void (^)(NSArray<YKDropdown> *))handler{
    NSMutableArray *views = [NSMutableArray array];
    
    while (data.count) {
        YKDropdownLabel *dropdownView = [[YKDropdownLabel alloc]initWithText:data.firstObject row:data selectionHandler:^(YKDropdownLabel *view, NSInteger index, id<YKDropdown> text) {
            if (view.subDropdownLabel) {
                view.subDropdownLabel.data = [view.data[index] sub];
                view.subDropdownLabel.text = view.subDropdownLabel.data.firstObject;
            }
            else {
                if (handler) {
                    handler([views tf_mapUsingBlock:^id _Nullable(YKDropdownLabel*  _Nonnull obj, NSInteger idx) {
                        return obj.text;
                    }]);
                }
            }
        }];
        dropdownView.backgroundColor = [UIColor tf_colorWithHexString:@"#f2f2f2"];
        [views addObject:dropdownView];
        data = [data.firstObject sub];
    }
    YKDropdownLabel *last;
    for (YKDropdownLabel *view in views.reverseObjectEnumerator) {
        view.subDropdownLabel = last;
        last = view;
    }
    
    return views;
    
}

@end
